<?php
abstract class paginator
{
	var $limit;
	var $lim;
	function __construct($limit)
	{
		$this->limit=$limit;

		if(isset($_GET['page']))
			$page=$_GET['page']-1;
		else
			$page=0;
		$this->lim=$page*$this->limit;
		$rowNum=$this->countRows();
		$page_num=ceil($rowNum/$this->limit);
		  function selfURL(){
		    if(!isset($_SERVER['REQUEST_URI']))    $suri = $_SERVER['PHP_SELF'];
		    else $suri = $_SERVER['REQUEST_URI'];
		    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
		    $sp=strtolower($_SERVER["SERVER_PROTOCOL"]);
		    $pr =    substr($sp,0,strpos($sp,"/")).$s;
		    $pt = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
		    return $pr."://".$_SERVER['SERVER_NAME'].$pt.$suri;
		    }
            $url=selfURL();
            $url=preg_replace("/(&page=)\w/", "", $url);
		echo ('
			<link rel="stylesheet" type="text/css" href="style/paginator.css" />
			<div class="page" align="center">Страницы: ');
			if($page_num>1)
		{
			$i=1;
			while($i<=$page_num)
			{
				$page=$i++;
				echo ('
			<a href="'.$url.'&page='.$page.'">'.$page.'</a>&nbsp;
			');
			}
		}
		else
		{
			echo ('
			1');
		}
		echo '</div>';
		$this->pageContent();
		echo ('
			<div class="page" align="center" style="border: 0px solid grey;
			 background-color: #eee; padding: 4px;">Страницы: ');
			if($page_num>1)
		{
			$i=1;
			while($i<=$page_num)
			{
				$page=$i++;
				echo ('
			<a href="'.$url.'&page='.$page.'">'.$page.'</a>&nbsp;
			');
			}
		}
		else
		{
			echo ('
			1');
		}
		echo '</div>';
	}
	abstract function countRows();
}
?>